package com.example.student.Controller;

import com.example.student.Repository.CourseRepository;
import com.example.student.Service.CourseService;
import com.example.student.entity.ApiResponse;
import com.example.student.entity.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/courses")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping("stAll")
    public CourseRepository listAllCourses() {
        List<Course> courses = courseService.listAll();
        return (CourseRepository) new ApiResponse(200, "获取课程信息成功", courses);
    }

    @GetMapping("/getById/{courseId}")
    public CourseRepository getCourseById(@PathVariable Long courseId) {
        Course course = courseService.getById(courseId);
        if (course != null) {
            return (CourseRepository) new ApiResponse(200, "获取课程信息成功", course);
        } else return (CourseRepository) new ApiResponse(404, "课程不存在", null);
    }

    @PostMapping("/add")
    public CourseRepository addCourse(@RequestBody Course course) {
        courseService.add(course);
        return (CourseRepository) new ApiResponse(201, "添加课程成功", null);
    }
}